package study.springboot.mvc.controller.rest;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import study.eggs.result.Result;
import study.eggs.result.Results;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/rest")
public class RestTemplateController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/requestFactory")
    public Result requestFactory() {
        ClientHttpRequestFactory factory = restTemplate.getRequestFactory();

        Map<String, Object> data = Maps.newHashMap();
        data.put("requestFactory", factory.getClass().getName());
        return Results.ok(data);
    }

    @GetMapping("/postRequest")
    public Result postRequest() {
        String body = restTemplate.getForObject("http://www.sohu.com", String.class);
        Map<String, Object> data = Maps.newHashMap();
        data.put("data", "");
        return Results.ok(data);
    }
}
